﻿using System;
using System.Security.Cryptography;
using System.Xml;
using System.IO;
using System.Text;
using System.Collections.Generic;


namespace Voize
{
    internal class CRemoteAuthentication
    {

        internal CRemoteAuthentication()
        {
            m_File = Environment.GetEnvironmentVariable("APPDATA") + @"\Voize\voize_auth.conf";
        }

        private string m_File;

        internal void AddUser(string password, string username)
        {
            //Prefer writing basic xml "manually" without XmlWriter.
            StreamWriter writer = new StreamWriter(m_File, true);
            Byte[] OriginalBytes;
            Byte[] EncodedBytes;
            string hash;
            MD5CryptoServiceProvider md5;

            OriginalBytes = ASCIIEncoding.Default.GetBytes(password);
            md5 = new MD5CryptoServiceProvider();
            EncodedBytes = md5.ComputeHash(OriginalBytes);
            hash = BitConverter.ToString(EncodedBytes);

            writer.WriteLine("\n<user>");
            writer.WriteLine("\t<name>" + username + "</name>");
            writer.WriteLine("\t<password>" + hash + "</password>");
            writer.WriteLine("</user>\n");
            writer.Close();
        }

        /// <summary>
        /// Password must be MD5 encoded.
        /// </summary>
        /// <returns>true if autentication was ok.</returns>
        internal bool Authenticate(string username, string password)
        {
            bool retval = false;
            XmlTextReader reader;
            List<string> NameList = new List<string>();
            List<string> HashList = new List<string>();
            int counter = 0;

            if (File.Exists(m_File))
            {
                reader = new XmlTextReader(m_File);
                
                while (reader.Read())
                {
                    switch (reader.NodeType)
                    {
                        case XmlNodeType.Element:
                            if (reader.Name == "name")
                                NameList.Add(reader.ReadInnerXml());
                            if (reader.Name == "password")
                                HashList.Add(reader.ReadInnerXml());
                            break;
                    }
                }

                foreach (string name in NameList)
                {
                    counter++;
                    if (name == username)
                    {
                        //OK user found. Check the password hash.
                        //Hashes and names should have the same index
                        if (password == HashList[counter - 1])
                            retval = true;
                    }
                }

                reader.Close();
            }
            else
                retval = false;

            NameList.Clear();
            HashList.Clear();

            return retval;

        }


        internal List<string> GetAllUsernames()
        {
            List<string> NameList = new List<string>();
            XmlTextReader reader;

            if (File.Exists(m_File))
            {
                reader = new XmlTextReader(m_File);

                while (reader.Read())
                {
                    switch (reader.NodeType)
                    {
                        case XmlNodeType.Element:
                            if (reader.Name == "name")
                                NameList.Add(reader.ReadInnerXml());
                            break;
                    }
                }

                reader.Close();
            }

            return NameList;

        }
    }
}
